package com.ibm.cps.message.validation;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.ibm.cps.message.MessageFields;
import com.ibm.util.ErrorCode;
import com.ibm.util.JSONUtility;
import com.ibm.util.exception.CPSException;

public class GroupValidator extends IProcessorValidator {

	@Override
	public void validatePostMessage(ObjectNode element) throws CPSException {
		String[] strFields = { "label" };
		ValidatorUtil.validateAllTypeFields(element, strFields, null, null,
				null);

		ValidatorUtil
				.assignRandomIdIfNotPresent(element, MessageFields.GROUPID);
	}

	@Override
	public String getProcessorid(ObjectNode object) throws CPSException {
		if (JSONUtility.isStringField(object, MessageFields.GROUPID)) {
			return JSONUtility.GetString(MessageFields.GROUPID, object, null);
		} else {
			throw new CPSException(ErrorCode.NULL_FIELD_ERROR,
					"Please provide the value of groupid in "
							+ object.toString());
		}

	}
}
